<?php

namespace app\admin\controller\entrepot;

use app\admin\model\Order;
use app\admin\model\other\Colour;
use app\admin\model\other\Processattribute;
use app\admin\model\Process;
use app\admin\model\statistics\Orderdetail;
use app\common\controller\Backend;
use http\Params;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use app\admin\controller\auth;

/**
 * 收货管理
 *
 * @icon fa fa-circle-o
 */
class Receiving extends Backend
{

    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = ['orderlist', 'get_list', 'get_abbreviation'];
    /**
     * Receiving模型对象
     * @var \app\admin\model\entrepot\Receiving
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\entrepot\Receiving;
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['order', 'detail', 'employee', 'attribute', 'colour', 'admin'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id', 'quantity', 'quantity_count', 'createtime', 'updatetime', 'status']);
                $row->visible(['order']);
                $row->getRelation('order')->visible(['order_no']);
                $row->visible(['detail']);
                $row->getRelation('detail')->visible(['trademark_printing', 'abbreviation', 'quantity', 'cumulative_quantity']);
                $row->visible(['employee']);
                $row->getRelation('employee')->visible(['name']);
                $row->visible(['attribute']);
                $row->getRelation('attribute')->visible(['name']);
                $row->visible(['colour']);
                $row->getRelation('colour')->visible(['name']);
                $row->visible(['admin']);
                $row->getRelation('admin')->visible(['nickname']);
                $row->group_id = $this->auth->getGroupIds()[0];
                $row->visible(['group_id']);
            }


            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }


        return $this->view->fetch();
    }


    /**
     * 添加 by lhq
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $orderDetialmodel = new Orderdetail();
                $result = false;
                Db::startTrans();
                try {
                    $processDetailArr = json_decode($params['process_detail'], true); //本次收取的工序列表

                    $insertData = [];
                    if (is_array($processDetailArr)) {
                        //获取订单主表数据
                        $order['received'] = get_order_received_by_id($params['order_id']);
                        foreach ($processDetailArr as $k => $v) {
//                            重新赋值订单色号收取总数
                            $orderDetail['colour_received'] = get_colour_received_by_id($v['order_detail_id']);

//                            p($orderDetail['colour_received']);
                            //获取订单明细表数据
                            $orderDetialWhere['id'] = $v['order_detail_id'];
                            $re = collection($orderDetialmodel->where($orderDetialWhere)->select())->toArray();
                            $orderDetialData = $re[0];
//                            p($orderDetialData);
                            if (!$orderDetialData['process_detail']) { //订单明细表中还未存在订单工序明细数据（订单第一次收货）
                                //获取当前商品所需工序信息
                                $process = new \app\admin\model\other\Process();
                                $p_w['id'] = $v['process_id'];
                                $p_re = collection($process->where($p_w)->limit(1)->select())->toArray();
                                if (count($p_re)) {
                                    $processList = explode(',', $p_re[0]['process_attribute_ids']);
//                                    $orderDetail['colour_received'] = $orderDetialData['quantity']*count(explode(',',$p_re[0]['process_attribute_ids'])); //色号收取总数 固定值
                                } else {
                                    $this->error('还未添加当前商品的生产工序');
                                }
                                $process_detail = [];
                                foreach ($processList as $k2 => $v2) {
                                    $new_data['process_attribute'] = $v2;
                                    $new_data['quantity'] = 0; //工序收取量
                                    $new_data['schedule'] = 0; //工序进度=工序累计收取量/订单总数*100
                                    $new_data['weights'] = 0; //工序权重 = 工序收取量/色号收取总数*工序进度/100
                                    $process_detail[] = $new_data;
                                }
                                //更新订单工序收取详情
                                $upOrderDetail['process_detail'] = json_encode($process_detail, JSON_UNESCAPED_UNICODE);
                            } else {
                                $process_detail = \GuzzleHttp\json_decode($orderDetialData['process_detail'], true);
                            }

                            //循环工序详情更新信息
//                            p($process_detail);
                            foreach ($process_detail as $k2 => $v2) {
                                if ($v2['process_attribute'] == $params['process_attribute_id']) {
                                    //判断收取量是否超过订单数量
                                    if($process_detail[$k2]['quantity'] + $v['current_quantity'] > $orderDetialData['quantity']){
                                        Db::rollback();
                                        $this->error('不能超单收货！检查后再提交');
                                    }

                                    //重新赋值工序累计收取数量
                                    $process_detail[$k2]['quantity'] += $v['current_quantity'];

//                                    p($process_detail[$k2]['quantity']);
                                    //重新赋值工序进度
                                    $process_detail[$k2]['schedule'] = $process_detail[$k2]['quantity'] / $orderDetialData['quantity'];

//                                    p($process_detail[$k2]['schedule']);
                                    //重新计算工序权重
                                    $process_detail[$k2]['weights'] = $process_detail[$k2]['quantity'] / $orderDetail['colour_received'] * $process_detail[$k2]['schedule'];

//                                    p($process_detail[$k2]['weights']);
                                    //更新订单详情工序累计收取总数
                                    $orderDetail['process_received'] = $orderDetialData['process_received'] + $v['current_quantity'];

//                                    p($orderDetail['process_received']);
//                                    更新订单详情色号进度
                                    $orderDetail['colour_schedule'] = get_colour_schedule($v['order_detail_id'], $v2['process_attribute'], $process_detail[$k2]['weights']);
//                                    p($orderDetail['colour_schedule']);
//                                    更新订单色号权重
                                    $orderDetail['colour_weights'] = $orderDetail['colour_received'] / $order['received'] * $orderDetail['colour_schedule'];

//                                    p($orderDetail['colour_weights']);
//


//                                  格式化比例数据
                                    foreach ($process_detail as $key => $val) {
                                        if ($val['process_attribute'] == $params['process_attribute_id']) {
                                            $process_detail[$key]['schedule'] = round($process_detail[$k2]['schedule'] * 100, 2);
                                            $process_detail[$key]['weights'] = round($process_detail[$k2]['weights'] * 100, 2);
                                        }
                                    }

//                                    [{"process_attribute":"1","quantity":1000000,"schedule":0,"weights":0},{"process_attribute":"2","quantity":0,"schedule":0,"weights":0},{"process_attribute":"3","quantity":0,"schedule":0,"weights":0}]
                                    $orderDetail['process_detail'] = json_encode($process_detail);
                                    $orderDetail['colour_schedule'] = round($orderDetail['colour_schedule'] * 100, 2);
                                    $orderDetail['colour_weights'] = round($orderDetail['colour_weights'] * 100, 2);
                                    $orderDetialmodel->where('id', 'eq', $orderDetialData['id'])->update($orderDetail);

                                    //新增收取记录
                                    $insertData[$k]['order_id'] = $params['order_id'];
                                    $insertData[$k]['order_detail_id'] = $orderDetialData['id'];
                                    $insertData[$k]['employee_id'] = $params['employee_id'];
                                    $insertData[$k]['process_attribute_id'] = $params['process_attribute_id'];
                                    $insertData[$k]['colour_id'] = $v['colour_id'];
                                    $insertData[$k]['quantity'] = $v['current_quantity'];
                                    $insertData[$k]['process_id'] = $v['process_id'];//工序id
                                }
                            }
                        }

                        //插入新收货纪录
                        $this->model->allowField(true)->saveAll($insertData);

                        $result = true;


                    }

                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }

                if ($result !== false || $result !== 0) {
                    //更新订单进度
                    up_order_schedule($params['order_id']);
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids, true);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $params['operator_uid'] = $this->auth->getUserInfo()['id']; //编辑后更新当前登录用户id

                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validateFailException(true)->validate($validate);
                    }

                    $oldRow = $row->toArray();
                    $result = $row->allowField(true)->save($params);

                    if ($result) {
                        $order['received'] = get_order_received_by_id($row['order_id']);
                        //                            重新赋值订单色号收取总数
                        $orderDetail['colour_received'] = get_colour_received_by_id($row['order_detail_id']);
                        //获取订单明细表数据
                        $orderDetialWhere['id'] = $row['order_detail_id'];
                        $orderDetialData = Db::name('order_detail')->where($orderDetialWhere)->find();
//                        p($orderDetialData);
                        $process_detail = \GuzzleHttp\json_decode($orderDetialData['process_detail'], true);

                        //循环工序详情更新信息
                        foreach ($process_detail as $k2 => $v2) {
                            if ($v2['process_attribute'] == $row['process_attribute_id']) {
                                //重新赋值工序累计收取数量
                                $process_detail[$k2]['quantity'] = $v2['quantity'] - $oldRow['quantity'] + $params['quantity'];

//                                p($process_detail[$k2]['quantity']);
//                                    p($process_detail[$k2]['quantity']);
                                //重新赋值工序进度
                                $process_detail[$k2]['schedule'] = $process_detail[$k2]['quantity'] / $orderDetialData['quantity'];
                                if ($process_detail[$k2]['schedule'] > 1) {
                                    Db::rollback();
                                    $this->error('收取数量已超单，请重新编辑提交！');
                                }

//                                    p($orderDetail['colour_received']);
                                //重新计算工序权重
                                if ($orderDetail['colour_received'] !== 0 && $process_detail[$k2]['schedule'] != 0) {
                                    $process_detail[$k2]['weights'] = $process_detail[$k2]['quantity'] / $orderDetail['colour_received'] * $process_detail[$k2]['schedule'];
                                }


//                                    p($process_detail[$k2]['weights']);
                                //更新订单详情工序累计收取总数
                                $orderDetail['process_received'] = $orderDetialData['process_received'] - $oldRow['quantity'] + $params['quantity'];

//                                    p($orderDetail['process_received']);
//                                    更新订单详情色号进度
                                $orderDetail['colour_schedule'] = get_colour_schedule($oldRow['order_detail_id'], $v2['process_attribute'], $process_detail[$k2]['weights']);
//                                    p($orderDetail['colour_schedule']);
//                                    更新订单色号权重
                                $orderDetail['colour_weights'] = $orderDetail['colour_received'] / $order['received'] * $orderDetail['colour_schedule'];

                                //更新订单进度
//                                $order['schedule'] = get_order_schedule($oldRow['order_id'],$oldRow['order_detail_id'],$orderDetail['colour_weights']);

                                //更新订单主表  订单收取总数、订单进度
                                up_order_schedule($oldRow['order_id']);

//                                $order['schedule'] = round($order['schedule']*100,2);
//                                $order['status'] = $order['schedule'] > 100 ? 0 : 1;
//                                $order_where['id'] = $oldRow['order_id'];
//
//                                Db::name('order')->where($order_where)->update($order);

//                                  格式化比例数据
                                foreach ($process_detail as $key => $val) {
                                    if ($val['process_attribute'] == $oldRow['process_attribute_id']) {
                                        $process_detail[$key]['schedule'] = round($process_detail[$k2]['schedule'] * 100, 2);
                                        $process_detail[$key]['weights'] = round($process_detail[$k2]['weights'] * 100, 2);
                                    }
                                }

//                                    [{"process_attribute":"1","quantity":1000000,"schedule":0,"weights":0},{"process_attribute":"2","quantity":0,"schedule":0,"weights":0},{"process_attribute":"3","quantity":0,"schedule":0,"weights":0}]
                                $orderDetail['process_detail'] = json_encode($process_detail);
                                $orderDetail['colour_schedule'] = round($orderDetail['colour_schedule'] * 100, 2);
                                $orderDetail['colour_weights'] = round($orderDetail['colour_weights'] * 100, 2);

                                Db::name('order_detail')->where('id', 'eq', $orderDetialData['id'])->update($orderDetail);

                            }
                        }

                    }

                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $row->abbreviation = Db::name('order_detail')->where('id', 'eq', $row->order_detail_id)->value('abbreviation');
        $row->process_attribute = Db::name('process_attribute')->where('id', 'eq', $row->process_attribute_id)->value('name');

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {

        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");

        if ($ids) {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();
//            p(collection($list)->toArray());
            $count = 0;
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {

                    $count += $v->delete();

                    //更新订单明细表的工序详情
                    $orderDetail = Db::name('order_detail')->where('id', 'eq', $v->order_detail_id)->find();
                    $order = Db::name('order')->where('id', 'eq', $orderDetail['order_id'])->find();
//var_dump($orderDetail);
//var_dump($order);

                    $processArr = \GuzzleHttp\json_decode($orderDetail['process_detail'], true);

                    foreach ($processArr as $k2 => $v2) {
//                        {"process_attribute":"1","quantity":0,"schedule":0,"weights":0}
                        if ($v2['process_attribute'] == $v->process_attribute_id) {

                            //重新赋值工序累计收取数量
                            $processArr[$k2]['quantity'] -= $v->quantity;

                            //重新赋值工序进度
                            $processArr[$k2]['schedule'] = $processArr[$k2]['quantity'] / $orderDetail['quantity'];

                            //重新计算工序权重
                            $processArr[$k2]['weights'] = $processArr[$k2]['quantity'] / $orderDetail['colour_received'] * $processArr[$k2]['schedule'];


                            //更新订单详情工序累计收取总数
                            $newOrderDetail['process_received'] = $orderDetail['process_received'] - $v->quantity;

//                                    更新订单详情色号进度
                            $newOrderDetail['colour_schedule'] = get_colour_schedule($v['order_detail_id'], $v2['process_attribute'], $processArr[$k2]['weights']);


//                                    更新订单色号权重
                            $newOrderDetail['colour_weights'] = $orderDetail['colour_received'] / $order['received'] * $newOrderDetail['colour_schedule'];

                            //更新订单进度
                            up_order_schedule($v->order_id);
//                            $newOrder['schedule'] = get_order_schedule($orderDetail['order_id'],$v['order_detail_id'],$newOrderDetail['colour_weights']);
//
//                            //更新订单主表  订单收取总数、订单进度
//                            $newOrder['schedule'] = round($newOrder['schedule']*100,2);
//
//                            $newOrder['status'] = $newOrder['schedule'] > 100 ? 0 : 1;
//
//                            $order_where['id'] = $v->order_id;
//
////                            var_dump($newOrder);
//                            Db::name('order')->where($order_where)->update($newOrder);

//                                  格式化比例数据
                            foreach ($processArr as $key => $val) {
                                if ($val['process_attribute'] == $v->process_attribute_id) {
                                    $processArr[$key]['schedule'] = round($processArr[$k2]['schedule'] * 100, 2);
                                    $processArr[$key]['weights'] = round($processArr[$k2]['weights'] * 100, 2);
                                }
                            }


                            $newOrderDetail['process_detail'] = json_encode($processArr);
                            $newOrderDetail['colour_schedule'] = round($newOrderDetail['colour_schedule'] * 100, 2);
                            $newOrderDetail['colour_weights'] = round($newOrderDetail['colour_weights'] * 100, 2);

//                            p($newOrderDetail);
                            //p(Db::name('order_detail')->where('id','eq',$v->order_detail_id)->fetchSql()->update($newOrderDetail));
                            Db::name('order_detail')->where('id', 'eq', $v->order_detail_id)->update($newOrderDetail);

                        }
                    }

                }

                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

    public function orderlist()
    {
        $colour = new Colour();
        //设置过滤方法
        $this->request->filter(['trim', 'strip_tags']);
        $params = $this->request->request();
        $result = array(
            "list" => [],
            'total' => 0
        );
        $page_arr = [];
        if ($params['type'] == 'order_id') {
            $where['order_no'] = array('like', '%' . $params['q_word'][0] . '%');
//            $where['status'] = '1';
            $data = Db::table('fa_order')
                ->where($where)
//                ->fetchSql()
                ->field(['id', 'order_no'])->select();

            $data_count = count($data);
            foreach ($data as $key => $val) {
                $page_arr[$key]['id'] = $data[$key]['id'];
                $page_arr[$key]['order_no'] = $data[$key]['order_no'];
            }
            $page_arr = formatArray($page_arr);
            $result = array(
                "list" => $page_arr,
                'total' => $data_count
            );
        }

        if ($params['type'] == 'trademark_printing') {
            if ($params['custom']['order_id']) {
                $where['order_id'] = $params['custom']['order_id'];
                $where['trademark_printing'] = array('like', '%' . $params['q_word'][0] . '%');
//                $where['status'] = '2';
                $data = Db::table('fa_order_detail')
                    ->where($where)
//                ->fetchSql()
                    ->field(['order_id', 'trademark_printing'])->select();
                $data_count = count($data);

                foreach ($data as $key => $val) {
                    $page_arr[$key]['id'] = $data[$key]['trademark_printing'];
                    $page_arr[$key]['trademark_printing'] = $data[$key]['trademark_printing'];
                }
                $page_arr = formatArray($page_arr);
                $result = array(
                    "list" => $page_arr,
                    'total' => $data_count
                );
            }
        }

        if ($params['type'] == 'colour') {
            if ($params['custom']['order_id'] && $params['custom']['trademark_printing']) {
                $where['order_id'] = $params['custom']['order_id'];
                $where['trademark_printing'] = $params['custom']['trademark_printing'];
//                $where['trademark_printing'] = array('like','%'.$params['q_word'][0].'%');
//                $where['status'] = '2';

                $data = Db::name('order_detail')
                    ->where($where)
//                ->fetchSql()
                    ->field(['colour_id', 'trademark_printing'])->select();
//                p($data);
                $data_count = count($data);

                foreach ($data as $key => $val) {
                    $page_arr[$key]['id'] = $data[$key]['colour_id'];
                    $page_arr[$key]['colour'] = $colour->where('id', 'eq', $data[$key]['colour_id'])->value('name');
                }
                $page_arr = formatArray($page_arr);
                $result = array(
                    "list" => $page_arr,
                    'total' => $data_count
                );
            }
        }

        if ($params['type'] == 'process') {
            if ($params['custom']['order_id'] && $params['custom']['trademark_printing'] && $params['custom']['colour_id']) {
                $colour_ids_arr = explode(',', $params['custom']['colour_id']);
//                          获取当前商品所需工序信息
                $process = new \app\admin\model\other\Process();
                $process_attribute = new Processattribute();
                $where['trademark_printing'] = $params['custom']['trademark_printing'];
                $order_process = collection($process->where($where)->select())->toArray();

//                            循环获取包含当前色号的工序
                $page_arr = [];
                $new = [];
                if (count($order_process) > 0) {
                    foreach ($order_process as $k => $v) {

                        $colour_arr = explode(',', $v['colour_ids']);

                        foreach ($colour_arr as $k2 => $v2) {
                            if (in_array($v2, $colour_ids_arr)) {
//                                p($colour_arr);
                                if (!$new) {
                                    $new = explode(',', $v['process_attribute_ids']);
                                } else {
                                    $new = array_intersect($new, explode(',', $v['process_attribute_ids']));
                                }
//                                foreach (explode(',', $v['process_attribute_ids']) as $k3=>$v3){
//                                    if(!in_array($v3,$page_arr)) {
//                                        $page_arr[$k3]['id'] = $v3;
//                                        $page_arr[$k3]['process'] = $process_attribute->where('id','eq',$v3)->value('name');
//                                    }
//                                }

                            }
                        }
                    }
                }

                if ($new) {
                    foreach ($new as $k3 => $v3) {
                        if (!in_array($v3, $page_arr)) {
                            $page_arr[$k3]['id'] = $v3;
                            $page_arr[$k3]['process'] = $process_attribute->where('id', 'eq', $v3)->value('name');
                        }
                    }
                }

                $data_count = count($page_arr);

                $page_arr = formatArray($page_arr);
                $result = array(
                    "list" => $page_arr,
                    'total' => $data_count
                );
            }
        }

        return json($result);
    }

    public function get_list()
    {
        if ($this->request->isPost()) {
            $params = $this->request->request();
            if ($params['process_attribute_id']) {
                $data = []; //订单号 、色号、订单数量、工序累计收取
                $orderDetailModel = new Orderdetail();

                $colourArr = explode(',', $params['colour_id']);
//                p($colourArr);
//            查询所有订单
                $where['order_id'] = $params['order_id'];
                $where['trademark_printing'] = $params['trademark_printing'];
                $where['colour_id'] = array('in', $colourArr);

                $orderDetailArr = collection($orderDetailModel->where($where)->select())->toArray();

                if (is_array($orderDetailArr)) {
                    foreach ($orderDetailArr as $k => $v) {
                        $item['order_detail_id'] = $v['id'];
                        $item['colour_id'] = $v['colour_id'];
                        $item['order_quantity'] = $v['quantity'];
                        $item['current_quantity'] = 0;
                        $item['process_received'] = 0;
                        $processDetail = json_decode($v['process_detail'], true);

                        if (is_array($processDetail) && count($processDetail) > 0) {
                            if (count($processDetail) > 0) {
                                foreach ($processDetail as $k2 => $v2) {
                                    if ($v2['process_attribute'] == $params['process_attribute_id']) {
                                        $item['process_received'] = $v2['quantity'];
                                    }
                                }
                            }
                        }
                        //                          获取当前商品所需工序信息
                        $process = new \app\admin\model\other\Process();
                        $process_attribute = new Processattribute();
                        $processWhere2['trademark_printing'] = $params['trademark_printing'];
                        $order_process = collection($process->where($processWhere2)->select())->toArray();

//                            循环获取包含当前色号的工序
                        if (count($order_process) > 0) {
                            foreach ($order_process as $k2 => $v2) {
                                $colour_arr = explode(',', $v2['colour_ids']);
                                foreach ($colour_arr as $k3 => $v3) {
                                    if (in_array($v3, $colourArr)) {
                                        $item['process_id'] = $v2['id'];
                                        break;
                                    }
                                }
                            }
                        }
                        $data[] = $item;
                    }
                }
                $this->success(null, '', json_encode($data));
            }


        }
        $this->error('获取数据失败');
    }

    /**
     * @return void
     * 结算
     */
    public function settle($ids = null)
    {
        $result = array(
            'type' => 0,
            'msg' => '操作失败！'
        );
        $data['data'] = [];
        $ids = explode(',', $ids);
        if (!$ids) return json($result,);

        //只有待结算状态才能退料
        $where['id'] = array('in', $ids);
        $where['status'] = array('neq', 0);
        $re = $this->model->where($where)->count();
        $result['msg'] = '只有待结算状态才能进行结算';
        if ($re > 0) return json($result);


        //更新状态
        $upWhere['id'] = array('in', $ids);
        $res = $this->model->where($upWhere)->setField('status', 1);

        $result['type'] = 1;
        $result['msg'] = '操作成功';
        return json($result);
    }

    public function get_abbreviation()
    {
        $result = '';
        if ($this->request->isPost()) {
            $param = $this->request->request();
            if ($param['order_id'] && $param['trademark_printing']) {
                $where['order_id'] = $param['order_id'];
                $where['trademark_printing'] = $param['trademark_printing'];
                $res = Db::name('order_detail')->where($where)->value('abbreviation');
                $result = $res ? $res : '';
//                $result = $param['order_id'].'||'.$param['trademark_printing'];
            }

        }
        return $result;
    }

}
